/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * The software in this package is published under the terms of the GPL v2.0
 * license, a copy of which has been included with this distribution in the
 * LICENSE-GPL.txt file.
 */

package org.cemantika.management;

import org.cemantika.contextconsumer.ContextConsumerAdapter;
import org.cemantika.exception.KnowledgeSessionNotCreatedException;
import org.cemantika.infrastructure.model.ContextUser;

/**
 * ContextualBehavior represents a class to deal with a contextual behavior that contains a focus
 * and possibly a contextual graph. The context that entails this contextual behavior is also
 * defined in this class.
 * 
 * @author andersonfellipe
 */
public class ContextualBehavior {

    /**
     * Represents the focus of a contextual behavior.
     */
    private ContextConsumerAdapter focus;
    
    /**
     * Represents the execution of a contextual behavior.
     */
    public void execute() throws KnowledgeSessionNotCreatedException {
        execute(null);
    }

    /**
     * Represents the execution of a contextual behavior.
     */
    public void execute(ContextUser contextUser) throws KnowledgeSessionNotCreatedException {
        ContextManager contextManager = ContextManager.getInstance();
        contextManager.getDroolsContextualManipulation(contextUser).startRules();
    }
    
    /**
     * Represents the execution of a contextual behavior until the halt of the contextual behavior or of the application.
     */
    public void executeForever() throws KnowledgeSessionNotCreatedException {
        executeForever(null);
    }
    
    /**
     * Represents the execution of a contextual behavior until the halt of the contextual behavior or of the application.
     */
    public void executeForever(ContextUser contextUser) throws KnowledgeSessionNotCreatedException {
        ContextManager contextManager = ContextManager.getInstance();
        contextManager.getDroolsContextualManipulation(contextUser).startRulesForever();
    }
    
    /**
     * Represents the stop of the execution of a contextual behavior.
     */
    public void stopExecution() throws KnowledgeSessionNotCreatedException {
        stopExecution(null);
    }
    
    /**
     * Represents the stop of the execution of a contextual behavior.
     */
    public void stopExecution(ContextUser contextUser) throws KnowledgeSessionNotCreatedException {
        ContextManager contextManager = ContextManager.getInstance();
        contextManager.getDroolsContextualManipulation(contextUser).stopRules();
    }

    /**
     * Assign the value of the field focus.
     * 
     * @param focus the focus to set.
     */
    public void setFocus(ContextConsumerAdapter focus) {
        this.focus = focus;
    }

}
